"""
颜色选择对话框
让玩家选择执黑（先手）或执白（后手）
"""

import pygame
import sys


class ColorChoiceDialog:
    """颜色选择对话框类"""
    
    def __init__(self, width=500, height=300):
        """
        初始化对话框
        :param width: 窗口宽度
        :param height: 窗口高度
        """
        self.width = width
        self.height = height
        
        # 初始化Pygame（如果还没初始化）
        if not pygame.get_init():
            pygame.init()
        
        self.screen = pygame.display.set_mode((self.width, self.height))
        pygame.display.set_caption("选择执棋颜色")
        self.clock = pygame.time.Clock()
        
        # 颜色定义
        self.COLOR_BG = (240, 240, 240)
        self.COLOR_TITLE = (50, 50, 50)
        self.COLOR_BLACK_BTN = (80, 80, 80)
        self.COLOR_BLACK_BTN_HOVER = (60, 60, 60)
        self.COLOR_WHITE_BTN = (220, 220, 220)
        self.COLOR_WHITE_BTN_HOVER = (200, 200, 200)
        self.COLOR_TEXT_BLACK = (255, 255, 255)
        self.COLOR_TEXT_WHITE = (50, 50, 50)
        self.COLOR_BORDER = (100, 100, 100)
        
        # 字体
        try:
            self.title_font = pygame.font.SysFont('microsoftyahei', 32, bold=True)
            self.button_font = pygame.font.SysFont('microsoftyahei', 28)
            self.desc_font = pygame.font.SysFont('microsoftyahei', 20)
        except:
            try:
                self.title_font = pygame.font.SysFont('simhei', 32, bold=True)
                self.button_font = pygame.font.SysFont('simhei', 28)
                self.desc_font = pygame.font.SysFont('simhei', 20)
            except:
                self.title_font = pygame.font.SysFont('arial', 32, bold=True)
                self.button_font = pygame.font.SysFont('arial', 28)
                self.desc_font = pygame.font.SysFont('arial', 20)
        
        # 创建按钮
        self.buttons = self.create_buttons()
        self.selected_color = None
        
    def create_buttons(self):
        """创建选择按钮"""
        button_width = 180
        button_height = 80
        spacing = 40
        start_x = (self.width - button_width * 2 - spacing) // 2
        button_y = 140
        
        buttons = {
            'black': {
                'rect': pygame.Rect(start_x, button_y, button_width, button_height),
                'text': '执黑（先手）',
                'desc': '黑棋先行',
                'color': 1
            },
            'white': {
                'rect': pygame.Rect(start_x + button_width + spacing, button_y, button_width, button_height),
                'text': '执白（后手）',
                'desc': '白棋后行',
                'color': 2
            }
        }
        return buttons
    
    def draw_dialog(self):
        """绘制对话框"""
        # 填充背景
        self.screen.fill(self.COLOR_BG)
        
        # 绘制标题
        title_text = "选择执棋颜色"
        title_surface = self.title_font.render(title_text, True, self.COLOR_TITLE)
        title_rect = title_surface.get_rect(center=(self.width // 2, 60))
        self.screen.blit(title_surface, title_rect)
        
        # 绘制按钮
        mouse_pos = pygame.mouse.get_pos()
        
        for choice, button_info in self.buttons.items():
            rect = button_info['rect']
            
            # 检查鼠标悬停
            is_hover = rect.collidepoint(mouse_pos)
            
            # 根据按钮类型选择颜色
            if choice == 'black':
                bg_color = self.COLOR_BLACK_BTN_HOVER if is_hover else self.COLOR_BLACK_BTN
                text_color = self.COLOR_TEXT_BLACK
            else:
                bg_color = self.COLOR_WHITE_BTN_HOVER if is_hover else self.COLOR_WHITE_BTN
                text_color = self.COLOR_TEXT_WHITE
            
            # 绘制按钮背景
            pygame.draw.rect(self.screen, bg_color, rect, border_radius=10)
            pygame.draw.rect(self.screen, self.COLOR_BORDER, rect, 3, border_radius=10)
            
            # 绘制按钮文字
            text_surface = self.button_font.render(button_info['text'], True, text_color)
            text_rect = text_surface.get_rect(center=(rect.centerx, rect.centery - 10))
            self.screen.blit(text_surface, text_rect)
            
            # 绘制描述文字
            desc_surface = self.desc_font.render(button_info['desc'], True, text_color)
            desc_rect = desc_surface.get_rect(center=(rect.centerx, rect.centery + 20))
            self.screen.blit(desc_surface, desc_rect)
    
    def handle_click(self, pos):
        """
        处理鼠标点击事件
        :param pos: 鼠标位置
        :return: 选择的颜色 (1为黑棋, 2为白棋) 或 None
        """
        for choice, button_info in self.buttons.items():
            if button_info['rect'].collidepoint(pos):
                return button_info['color']
        return None
    
    def run(self):
        """运行对话框主循环"""
        running = True
        
        while running:
            # 处理事件
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    return None  # 用户关闭窗口
                elif event.type == pygame.MOUSEBUTTONDOWN:
                    if event.button == 1:  # 左键点击
                        color = self.handle_click(event.pos)
                        if color:
                            self.selected_color = color
                            return color
            
            # 绘制界面
            self.draw_dialog()
            pygame.display.flip()
            self.clock.tick(60)
        
        return None
